package org.absolutezero.client.presenter;

import javax.inject.Inject;

import org.absolutezero.client.events.NavigationChangeEvent;
import org.absolutezero.client.events.NavigationChangeEventHandler;
import org.absolutezero.client.events.RemoveShelfEvent;
import org.absolutezero.client.events.RemoveShelfEventHandler;
import org.absolutezero.client.events.SaveForeignShelfEvent;
import org.absolutezero.client.events.SaveForeignShelfEventHandler;
import org.absolutezero.client.events.SaveShelfEvent;
import org.absolutezero.client.events.SaveShelfEventHandler;
import org.absolutezero.client.events.ViewShelfEvent;
import org.absolutezero.client.events.ViewShelfEventHandler;
import org.absolutezero.client.util.Dialog;
import org.absolutezero.client.view.ShelvesView;
import org.absolutezero.shared.RefrigeratorRequestFactory;
import org.absolutezero.shared.ShelfProxy;
import org.absolutezero.shared.ShelfRequest;

import com.google.gwt.event.shared.EventBus;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;
import com.google.web.bindery.requestfactory.shared.ServerFailure;

public class ShelvesPresenter {

	private final ShelvesView shelves;
	private final EventBus ebus;
	private final RefrigeratorRequestFactory requestFactory;
	
	@Inject
	public ShelvesPresenter(ShelvesView shelves,
						EventBus ebus,
						RefrigeratorRequestFactory requestFactory) {
		
		this.ebus = ebus;
		this.shelves = shelves;
		this.requestFactory = requestFactory;
		
		bind();
	}
	
	private void bind(){
		ebus.addHandler(NavigationChangeEvent.TYPE, new NavigationChangeEventHandler() {
			
			@Override public void onEvent(NavigationChangeEvent e) { onNavigationChange(e); }
		});
		
		ebus.addHandler(SaveShelfEvent.TYPE, new SaveShelfEventHandler() {
			
			@Override public void onEvent(SaveShelfEvent e) { onSaveShelf(e); }
		});

		ebus.addHandler(RemoveShelfEvent.TYPE, new RemoveShelfEventHandler() {
	
			@Override public void onEvent(RemoveShelfEvent e) { onRemoveShelf(e); }
		});
		ebus.addHandler(SaveForeignShelfEvent.TYPE, new SaveForeignShelfEventHandler() {
			
			@Override public void onEvent(SaveForeignShelfEvent e) {  onSaveForeignShelf(e); }
		});
		ebus.addHandler(ViewShelfEvent.TYPE, new ViewShelfEventHandler() {
			
			@Override public void onEvent(ViewShelfEvent e) {  onViewShelf(e); }
		});
	}
	
	private void onNavigationChange(NavigationChangeEvent e){
				
		switch(e.getNavigationItemSelected()){
		case SHELF_MY:
			shelves.setMyShelves();
			break;
			
		case SHELF_BROWSE:
			shelves.setBrowseShelves();
			break;
		}
	}
	
	private void onSaveShelf(SaveShelfEvent e){
		shelves.setEditShelf(e.getShelf());
	}
	
	private void onRemoveShelf(RemoveShelfEvent e){
		ShelfProxy shelf  = e.getShelf();
		
		ShelfRequest request = requestFactory.shelfRequest();
		shelf = request.edit(shelf);
		Request<Void> removeShelfRequest = request.remove().using(shelf);
		
		removeShelfRequest.fire(new Receiver<Void>(){

			@Override
			public void onSuccess(Void response) {
				Dialog.show("Shelf remove", "success");
				shelves.setMyShelves();
			}
			
			@Override
			public void onFailure(ServerFailure f){
				Dialog.show("Shelf remove failed",f.getMessage());
			}
			
		});
	}
	
	private void onSaveForeignShelf(SaveForeignShelfEvent e){
		shelves.setEditForeignShelf(e.getShelf());
	}
	
	private void onViewShelf(ViewShelfEvent e){
		shelves.setViewShelf(e.getShelf());
	}
}
